<?php

namespace backend\modules\goods\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Goods;

/**
 * GoodsSearch represents the model behind the search form about `common\models\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_id', 'goods_name', 'goods_image', 'gall_images_id', 'desc_images_id', 'cate_id'], 'safe'],
            [['shop_price', 'market_price'], 'number'],
            [['is_new', 'is_hot', 'is_good', 'is_promote', 'is_delete','is_single'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $params = isset($params['GoodsSearch'])?$params['GoodsSearch']:Yii::$app->request->post();
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'shop_price' => $this->shop_price,
            'market_price' => $this->market_price,
            'is_new' => $this->is_new,
            'is_hot' => $this->is_hot,
            'is_good' => $this->is_good,
            'is_promote' => $this->is_promote,
            'is_delete' => $this->is_delete,
        ]);

        $query->andFilterWhere(['like', 'goods_id', $this->goods_id])
            ->andFilterWhere(['like', 'goods_name', $this->goods_name])
            ->andFilterWhere(['like', 'goods_image', $this->goods_image])
            ->andFilterWhere(['like', 'gall_images_id', $this->gall_images_id])
            ->andFilterWhere(['like', 'desc_images_id', $this->desc_images_id])
            ->andFilterWhere(['like', 'cate_id', $this->cate_id]);

        return $dataProvider;
    }
}
